/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.pstn;

import java.awt.*;

import javax.swing.*;

import demo.DemoPane;

import twaver.*;
import twaver.network.*;
import twaver.network.background.ColorBackground;
import twaver.table.*;

public class PSTNDemo extends DemoPane {
    TDataBox box = new TDataBox();
    TNetwork network = new TNetwork(box);
    TPropertySheet sheet = new TPropertySheet(box);
    private JSplitPane split = new JSplitPane();
    private boolean isAdjusting = false; 
    
    public PSTNDemo() {
        sheet.setEditable(true);
        split.setContinuousLayout(true);
        split.setDividerLocation(720);
        split.add(network, JSplitPane.LEFT);
        split.add(new TPropertySheetPane(sheet), JSplitPane.RIGHT);

        this.add(split, BorderLayout.CENTER);
        
        TopologyDataFactory.createData(box);
        network.setToolbarByName(TWaverConst.DEFAULT_TOOLBAR);
        
		ColorBackground background = new ColorBackground(Color.cyan);
		background.setGradientColor(Color.black);
		background.setGradientFactory(TWaverConst.GRADIENT_LINE_S);
		background.setGradient(true);
		network.setBackground(background);
        
        network.setBlinkingRule(new BlinkingRule(){
			public boolean isBodyBlinking(Element element) {
				return element.getAlarmState().getHighestNativeAlarmSeverity() != null ||
				        element.getClientProperty(TWaverConst.PROPERTYNAME_RENDER_COLOR) != null;
			}
			public boolean isOutlineBlinking(Element element) {
				return element.getAlarmState().getPropagateSeverity() != null ||
		        	element.getClientProperty(TWaverConst.PROPERTYNAME_STATE_OUTLINE_COLOR) != null;
			}
        });
        
        box.getSelectionModel().addDataBoxSelectionListener(new DataBoxSelectionListener(){
			public void selectionChanged(DataBoxSelectionEvent e) {
				if(isAdjusting){
					return;
				}
				if(box.getSelectionModel().size() > 1){
					javax.swing.SwingUtilities.invokeLater(new Runnable(){
						public void run() {
							if(box.getSelectionModel().size() > 1){
								isAdjusting = true;
								box.getSelectionModel().setSelection(box.getSelectionModel().lastElement());
								isAdjusting = false;
							}
						}
					});
				}	
			}
        });
        
    }

}